package com.cloudkinto.controller.replacement;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.vo.CostRecordListRes;
import com.cloudkinto.service.replacement.ReplacementService;
import com.cloudkinto.service.replacement.impl.vo.ReplacementAddReq;
import com.cloudkinto.service.replacement.impl.vo.ReplacementExportReq;
import com.cloudkinto.service.replacement.impl.vo.ReplacementUpdateReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/2/20 14:26
 **/
@RestController
@Api(tags = "补发")
@RequestMapping("/api/replacement")
public class ReplacementController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReplacementController.class);

    @Autowired
    ReplacementService service;

    //将主表中的某些信息带过来
    @UserSessionRequired
    @GetMapping("/addInit/{outputId}")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit(@PathVariable("outputId") Long outputId) {
        return service.addInit(outputId);
    }

    @UserSessionRequired
    @PostMapping("/add")
    @ApiOperation(value = "添加", notes = "添加")
    public SingleResult add(@RequestBody ReplacementAddReq replacementAddReq) {
        Long currentUserId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.add(currentUserId, replacementAddReq,companyId);
    }

    @UserSessionRequired
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody ReplacementUpdateReq replacementUpdateReq) {
        Long currentUserId = getCurrentUserId();
        return service.updateR(currentUserId, replacementUpdateReq);
    }

    @UserSessionRequired
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除", notes = "删除")
    public SingleResult delete(@PathVariable("id") @Validated Long id) {
        Long currentUserId = getCurrentUserId();
        return service.delete(currentUserId, id);
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = CostRecordListRes.class)})
    public PageResult listPage(String pageSize, String current, String keyword, String shopId, String storageId, String startDate, String endDate) {
        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("outputShopId", shopId);
        map.put("outputStorageId", storageId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        if (getCompanyId() != null)
            map.put("companyId", getCompanyId() + "");
        return service.getListPage(map);
    }


    @UserSessionRequired
    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出补发信息", notes = "导出补发信息")
    public void exportExcel(@RequestBody ReplacementExportReq request, HttpServletResponse response) throws IOException {
        //校验
        getCurrentUserId();
        service.exportExcel(request.getIds(), response);
    }


    @UserSessionRequired
    @GetMapping("confirmPhone")
    @ApiOperation(value = "确认照片")
    public SingleResult confirmPhone(Long id) {
        service.confirmPhone(id, getCurrentUserId());
        return SingleResult.success();
    }

}
